import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [
    { path: '/', redirect: '/shop/home' },

    {
        path: '/shop',
        component: () =>
            import ('@/index.vue'),
        children: [{
                path: 'home',
                name: 'Home',
                component: () =>
                    import ('@/views/home.vue'),
            },
            {
                path: 'cart',
                name: 'Cart',
                component: () =>
                    import ('@/views/cart.vue'),
            },
            {
                path: 'shopping',
                name: 'Shopping',
                component: () =>
                    import ('@/views/shopping.vue'),
            },
            {
                path: 'bookdetail/:id',
                name: 'Bookdetail',
                component: () =>
                    import ('@/views/bookdetail.vue'),
            },
            {
                path: 'checkout',
                name: 'Checkout',
                component: () =>
                    import ('@/views/checkout.vue'),
            },
            {
                path: 'wishlish',
                name: 'Wishlish',
                component: () =>
                    import ('@/views/wishlish.vue'),
            },
            {
                path: 'account',
                name: 'Account',
                component: () =>
                    import ('@/views/account.vue'),
            },
            {
                path: 'email',
                name: 'Email',
                component: () =>
                    import ('@/views/email.vue'),
            },
            {
                path: 'footprint',
                name: 'Footprint',
                component: () =>
                    import ('@/views/footprint.vue'),
            },
            {
                path: 'order',
                name: 'Order',
                component: () =>
                    import ('@/views/order.vue'),
            },
            {
                path: 'chat',
                name: 'Chat',
                component: () =>
                    import ('@/components/chat.vue'),
            },


        ]

    },
    {
        path: '/login',
        name: 'Login',
        component: () =>
            import ('@/components/login.vue'),
    },
    {
        path: '/register',
        name: 'Register',
        component: () =>
            import ('@/components/register.vue'),
    },

]
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes
})


// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
    // to 将要访问的路径
    // from 代表从哪个路径跳转而来
    // next 是一个函数，表示放行
    //     next()  放行    next('/login')  强制跳转

    if (to.path === '/login') return next()
    if (to.path === '/register') return next()
    if (to.path === '/shop/email') return next()
    if (to.path === '/') return next()
    if (to.path === '/shop/home') return next()
    if (to.path === '/shop/shopping') return next()
    if (to.path === '/shop/bookdetail/:id') return next()

    // 获取token
    const user = window.sessionStorage.getItem('user1')
    if (!user) return next('/login')
    next()
})

export default router